<?php
/**
 * OWL Widget Factory Class
 *
 * @package OWL
 * @subpackage Widget
 * @since 1.0
 * @author trung
 */
class Owl_Widget_Factory {

    private $registered_widgets;
    public function register_widget( $widget_class ) {
        $widget = & new $widget_class();
        $this->registered_widgets[$widget->id_base] = $widget;
    }

    public function show_widget($widget_id,$display_args,$editable = false) {
        $widget = split('-', $widget_id);
        if ($this->registered_widgets[$widget[0]] != null){
            //$display_args['before_widget'] = sprintf($display_args['before_widget'],$widget_id,$this->registered_widgets[$widget[0]]->widget_classname);
            if ($editable) {
                echo "<div id='widget-$widget_id' class='widget-container'>";
                $this->registered_widgets[$widget[0]]->build_editable_display($widget[1],$display_args);
                echo '</div>';
            } else {
                $this->registered_widgets[$widget[0]]->build_display($widget[1],$display_args);
            }
        }
    }

    public function rebuild_widget($widget_id, $sidebar_id){
        global $owl_sidebar_control;
        $widget = split('-', $widget_id);
        $sidebar = $owl_sidebar_control->get_registered_sidebars();
        $display_args = $sidebar[$sidebar_id];
        return $this->registered_widgets[$widget[0]]->build_editable_display($widget[1],array_slice($display_args,4,6));
    }
    public function widget_form($widget_id) {
        $widget = split('-', $widget_id);
        echo $this->registered_widget[$widget[0]]->build_form($widget[1]);
    }
    
    public function get_list_widget() {
        echo '<div class="poof"></div>';
        echo '<div id="widget-list">';
        $sort = $this->registered_widgets;
        usort( $sort, create_function( '$a, $b', 'return strnatcasecmp( $a->name, $b->name);' ) );
        $count=0;
        foreach ($sort as $widget) {
            $count++;            
            $widget->build_display_template();
            if ($count % 2 == 0) {
                echo '<div style="clear:both;"></div>';
            }
        }
        echo '<div style="clear:both;"></div>';
        echo '</div>';
        echo '<div style="clear:both;"></div>';
    }  

    public function get_widget_control($widget_id) {
        $widget = split('-', $widget_id);
        //if ($widget[1] == "_T_"){
            //$widget[1] = $this->get_next_number($widget[0]);
        //}
        //echo "<div id='widget-$widget[0]-$widget[1]' class='widget-container'>";
        return $this->registered_widgets[$widget[0]]->build_form($widget[1]);
        //echo "</div>";
    }

    public function get_sidebar_control($sidebar) {
        global $owl_sidebar_control;
        //echo "\t<div id='$sidebar' class='widgets-sortables' style='min-height:200px;'>\n";
        $sidebar = $owl_sidebar_control->get_sidebar_widget($sidebar);
        if (is_array($sidebar)) {
            foreach ($sidebar as $widget) {
                $widgets = split('-', $widget);
                $args = array (
                        'id_base' => $widgets[0],
                        'number'  => $widgets[1],
                        'widget_id' => $widget
                );
                $args = $this->get_dynamic_param($args);
                $this->get_widget_control($args);
            }
        }
        //echo "\t</div>\n";
    }
    
    public function get_next_number($id_base) {
        $number = 1;
        $sidebar_widget = get_option('sidebars_widgets',array());
        while (!Owl_System_Helper::in_array($id_base.'-'.$number,$sidebar_widget)) {
            $number++;
        }
        return $number;
    }
    public function get_registered_widgets() {
        return $this->registered_widgets;
    }

    public function retrieve_widgets() {
            global $owl_sidebar_control;
            $_sidebars_widgets = array();
            $sidebars = array_keys($owl_sidebar_control->get_registered_sidebars());
            $old = array_keys($owl_sidebar_control->get_sidebar_widget());
            $sidebars_widgets = $owl_sidebar_control->get_sidebar_widget();            
            sort($old);
            sort($sidebars);
            if ( $old == $sidebars )
                    return;

            foreach ( $sidebars as $id ) {
                    if ( array_key_exists( $id, $sidebars_widgets ) ) {
                            $_sidebars_widgets[$id] = $sidebars_widgets[$id];
                            unset($sidebars_widgets[$id], $sidebars[$id]);
                    }
            }            
            if ( !empty($sidebars_widgets) ) {
                    foreach ( $sidebars_widgets as $lost => $val ) {
                            if ( is_array($val) )
                                    $_sidebars_widgets['owl_inactive_widgets'] = array_merge( (array) $_sidebars_widgets['owl_inactive_widgets'], $val );
                    }
            }           
            $sidebars_widgets = $_sidebars_widgets;
            unset($_sidebars_widgets);            
            $owl_sidebar_control->save_sidebars_widgets($sidebars_widgets);
        }
        
        public function fix_sidebar_widget(){
            global $owl_sidebar_control;
            
            $shown_widgets = array();
            $sidebars_widgets = $owl_sidebar_control->get_sidebar_widget();
            foreach ( $sidebars_widgets as $sidebar => $widgets ) {
                    if ( !is_array($widgets) )
                            continue;

                    $_widgets = array();
                    foreach ( $widgets as $widget ) {
                            $widget_id = split('-', $widget);
                            if ( isset($this->registered_widgets[$widget_id[0]]) )
                                    $_widgets[] = $widget;
                    }
                    $sidebars_widgets[$sidebar] = $_widgets;
                    $shown_widgets = array_merge($shown_widgets, $_widgets);
            }
            unset($_widgets);
            $owl_sidebar_control->save_sidebars_widgets($sidebars_widgets);
        }       
        
}
/**
 * API
 */
function register_widget($widget_class) {
    global $owl_widget_control;
    $owl_widget_control->register_widget($widget_class);
}

function retrieve_widgets(){
    global $owl_widget_control;
    $owl_widget_control->retrieve_widgets();
}

function fix_sidebar_widget(){
    global $owl_widget_control;
    $owl_widget_control->fix_sidebar_widget();
}

function register_default_widgets() {
    require_once CLASS_PATH . 'owl-default-widgets.php';
    
    $dir_handle = opendir(WIDGETS_PATH);
    if ($dir_handle === false) {
        die('Cannot find widgets path, please make sure that path to <code>widgets</code> exist ');
    }
    do {
        $file = readdir($dir_handle);
        if ($file != null && $file[0] != '.' && is_file(WIDGETS_PATH.$file)) {
            $file_info = file_get_contents(WIDGETS_PATH.$file);
            if (strpos($file_info,"OWL Widget File") !== false) {
                require_once (WIDGETS_PATH.$file);
            }
        }
    }
    while ($file != null);
    closedir($dir_handle);
}
?>
